﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.WebApp.KM;
using ZX.SYS.Library;
using ZX.SYS.Bussiness.DeskTop;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using ZX.DAL.DBExtend;

namespace ZX.WebApp.DeskTop.Form
{
    public partial class DesktopConfig : KmBasePage
    {
        private string _Action = string.Empty;
        private string RoleID = string.Empty;

        public string FormID
        {
            get { return Guid.NewGuid().ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _Action = QueryString("Action");
            switch (_Action)
            {
                case "savedata":
                    SaveData();
                    break;
                case "LoadData":
                    LoadData();
                    break;
            }
        }

        protected void SaveData()
        {
            string _Action = QueryString("_Action");
            string data = QueryString("mydata");
            string RoleID = Request["RoleID"];

            DeskTopModel m = new DeskTopModel();
            if (!string.IsNullOrEmpty(data))
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(data)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(m.GetType());
                    m = (DeskTopModel)serializer.ReadObject(ms);
                }
            }

            m.title = HttpUtility.UrlDecode(m.title);
            m.tools = HttpUtility.UrlDecode(m.tools);

            if (m.sort == "" || m.sort == "undefined") m.sort = "999";

            SysDesktopTemplate sdt = new SysDesktopTemplate();
            sdt = _bll.GetEntityByCondition<SysDesktopTemplate>("DeskTopTempID='" + m.id + "'");
            if (sdt == null) sdt = new SysDesktopTemplate();
            sdt.DeskTopTempID = new Guid(m.id);
            m.id = sdt.DeskTopTempID.ToString();
            sdt.DeskTopTempName = m.title;
            sdt.Src = m.src;
            sdt.Height = "";
            sdt.Region = m.Region;
            sdt.Sort = int.Parse(m.sort);
            sdt.PageType = int.Parse(m.pageType);
            if (m.RoleDesktopID == "") sdt.PowerType = int.Parse(m.powerType);

            sdt.TabTools = m.tools;
            sdt.More = m.more;
            BDeskTop.ModifyDeskTopTemplate(sdt, _Action);

            if (_Action != "add")
            {
                if (m.RoleDesktopID != "")
                {
                    string strsql = "update SysRoleDesktop set PowerType=" + m.powerType + " where RoleDesktopID='" + m.RoleDesktopID + "'";
                    DbSql execsql = new DAL.DBExtend.DbSql();
                    execsql.ExecuteSql(strsql);
                }
            }

            ResponseJson(m);
        }

        protected void LoadData()
        {
            string ID = QueryString("ID");
            string RoleID = Request["RoleID"];

            SysDesktopTemplate sdt = new SysDesktopTemplate();
            sdt = BDeskTop.GetSysDesktopTemplateEntity(ID, RoleID);
            if (sdt == null) sdt = new SysDesktopTemplate();
            DeskTopModel dtm = new DeskTopModel
            {
                id = sdt.DeskTopTempID.ToString(),
                title = sdt.DeskTopTempName,
                src = sdt.Src,
                pageType = sdt.PageType.ToString(),
                powerType = sdt.PowerType.ToString(),
                tools = sdt.TabTools,
                Region = sdt.Region,
                more = sdt.More
            };
            ResponseJson(dtm);
        }
    }
}